import requests
from lxml import etree

url='https://sou.zhaopin.com/?jl=530&sf=0&st=0&kw=%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95&kt=3'

header = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36"
    }

response = requests.get(url=url, headers=header)

html_str = response.text

    # 转换
html = etree.HTML(html_str)

# 提取所有的岗位节点
# job_list = html.xpath('//*[@id="listItemPile"]/div[2]')

job_list = html.xpath('//div[@class="contentpile"]/div')
# print(job_list)

for job in job_list:
    # 获取岗位名称

    job_name = job.xpath('//*[@id="listContent"]/div[1]/div/a/div[1]/div[1]/span')

    job_name2 = job.xpath('//*[@id="listContent"]/div[1]/div/a/div[1]/div[1]/span/span')
    print(job_name2)

    # # 获取薪资待遇
    # price = job.xpath('./div/strong/text()')
    # print(price)

    # # 获取公司名称
    job_com = job.xpath('.//*[@id="listContent"]/div/div/a/div/div/a/text()')
    print(job_com)
    #
    # # 格式化一下数据
    # job_data = '岗位名称：{}  薪资：{}  公司：{}'.format(job_name[0], price[0], job_com[0])
    # print(job_data)
    #
    # #  将提取的数据写入文件
    # with open('job_data.txt', 'a', encoding='utf8') as f:
    #     f.write(job_data + '\n')
